#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>



#define NEWCHARLED_CNT    1 //设备号
#define NEWCHARLED_NAME   "newcharled" //设备名
#define LEDOFF            0  //关灯
#define LEDON             1  //开灯

//寄存器物理地址
#define CCM_CCGR1_BASE				(0X020C406C)	
#define SW_MUX_GPIO1_IO03_BASE		(0X020E0068)
#define SW_PAD_GPIO1_IO03_BASE		(0X020E02F4)
#define GPIO1_DR_BASE				(0X0209C000)
#define GPIO1_GDIR_BASE				(0X0209C004)

//映射后寄存器虚拟地址
static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;

//  newchar设备结构体
typedef struct newcharled_dev {
    dev_t devid;              //设备号
    struct cdev cdev;        //设备结构体
    struct class *class;    // 设备类
    struct device *device;   // 设备
    int major;              //主设备号
    int minor;              //次设备号 

}newcharled_dev;

newcharled_dev newcharled; //led设备



// led开关函数

void led_switch(unsigned char sta)
{
	unsigned int  val = 0;
	if(sta == LEDON) {
		val = readl(GPIO1_DR);
		val &= ~(1 << 3);	
		writel(val, GPIO1_DR);
	}else if(sta == LEDOFF) {
		val = readl(GPIO1_DR);
		val|= (1 << 3);	
		writel(val, GPIO1_DR);
	}	
}

// open

static int led_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &newcharled;
    return 0;
}

//read

static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
    return 0;
}


//write

static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int retvalue = 0;
    unsigned char databuf[1];
    unsigned ledstat;
    
    retvalue = copy_from_user(databuf, buf, cnt);
    if (retvalue < 0)
    {
        printk("kernel write failed /r/n");
        return -EFAULT;
    }

    ledstat = databuf[0];
    
    led_switch(ledstat);
    return 0;
}

// close 
static int led_release(struct inode *inode, struct file* filp)
{
    return 0;
}

// 设备操作函数

static const struct file_operations newcharled_op = {

    .owner = THIS_MODULE,
    .open  = led_open,
    .read  = led_read,
    .write = led_write,
    .release  = led_release
};


// device init 

static int __init led_init(void)
{
    unsigned int val = 0;
    /* 初始化LED */
	/* 1、寄存器地址映射 */
  	IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
	SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE, 4);
  	SW_PAD_GPIO1_IO03 = ioremap(SW_PAD_GPIO1_IO03_BASE, 4);
	GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);
	GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4);

	/* 2、使能GPIO1时钟 */
	val = readl(IMX6U_CCM_CCGR1);
	val &= ~(3 << 26);	/* 清楚以前的设置 */
	val |= (3 << 26);	/* 设置新值 */
	writel(val, IMX6U_CCM_CCGR1);

	/* 3、设置GPIO1_IO03的复用功能，将其复用为
	 *    GPIO1_IO03，最后设置IO属性。
	 */
	writel(5, SW_MUX_GPIO1_IO03);
	
	/*寄存器SW_PAD_GPIO1_IO03设置IO属性
	 *bit 16:0 HYS关闭
	 *bit [15:14]: 00 默认下拉
     *bit [13]: 0 kepper功能
     *bit [12]: 1 pull/keeper使能
     *bit [11]: 0 关闭开路输出
     *bit [7:6]: 10 速度100Mhz
     *bit [5:3]: 110 R0/6驱动能力
     *bit [0]: 0 低转换率
	 */
	writel(0x10B0, SW_PAD_GPIO1_IO03);

	/* 4、设置GPIO1_IO03为输出功能 */
	val = readl(GPIO1_GDIR);
	val &= ~(1 << 3);	/* 清除以前的设置 */
	val |= (1 << 3);	/* 设置为输出 */
	writel(val, GPIO1_GDIR);

	/* 5、默认关闭LED */
	val = readl(GPIO1_DR);
	val |= (1 << 3);	
	writel(val, GPIO1_DR);
    
    //注册字符设备驱动
    //1 创建设备号

    if (newcharled.major)
    {
        newcharled.devid = MKDEV(newcharled.major, 0);
        register_chrdev_region(newcharled.devid, NEWCHARLED_CNT, NEWCHARLED_NAME);
    
    }
    else
    {
        alloc_chrdev_region(&newcharled.devid, 0, NEWCHARLED_CNT, NEWCHARLED_NAME);
        newcharled.major = MAJOR(newcharled.devid);
        newcharled.minor = MINOR(newcharled.minor);
    }
    printk("newcharled  major = %d, minor = %d \r\n", newcharled.major, newcharled.minor);

    //初始化cdev
    newcharled.cdev.owner = THIS_MODULE;
    cdev_init(&newcharled.cdev, &newcharled_op);
    
    //添加进linux
    cdev_add(&newcharled.cdev, newcharled.devid, NEWCHARLED_CNT);

    //创建类
    newcharled.class = class_create(THIS_MODULE, NEWCHARLED_NAME);

    //创建设备
    newcharled.device = device_create(newcharled.class, NULL, newcharled.devid, NULL, NEWCHARLED_NAME);

    return 0;
}

static void __exit led_exit(void)
{
    // 取消虚拟地址的映射
    iounmap(IMX6U_CCM_CCGR1);
	iounmap(SW_MUX_GPIO1_IO03);
	iounmap(SW_PAD_GPIO1_IO03);
	iounmap(GPIO1_DR);
	iounmap(GPIO1_GDIR);

    //注销字符设备

    // 首先，从系统中删除字符设备  
    cdev_del(&newcharled.cdev);

    // 然后，注销分配给该设备的设备号 
    unregister_chrdev_region(newcharled.devid, NEWCHARLED_CNT);

    // 接着，销毁与设备号关联的设备文件  
    device_destroy(newcharled.class, newcharled.devid);

    // 最后，销毁设备类 
    class_destroy(newcharled.class);

    return;

}


module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("shiyue");

